package org.dromara.canteen.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.canteen.domain.vo.CanteenCertificatesVo;
import org.dromara.canteen.domain.bo.CanteenCertificatesBo;
import org.dromara.canteen.service.ICanteenCertificatesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 证书管理
 *
 * @author Gjb
 * @date 2025-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/canteen/certificates")
public class CanteenCertificatesController extends BaseController {

    private final ICanteenCertificatesService canteenCertificatesService;

    /**
     * 查询证书管理列表
     */
    @SaCheckPermission("canteen:certificates:list")
    @GetMapping("/list")
    public TableDataInfo<CanteenCertificatesVo> list(CanteenCertificatesBo bo, PageQuery pageQuery) {
        return canteenCertificatesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出证书管理列表
     */
    @SaCheckPermission("canteen:certificates:export")
    @Log(title = "证书管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CanteenCertificatesBo bo, HttpServletResponse response) {
        List<CanteenCertificatesVo> list = canteenCertificatesService.queryList(bo);
        ExcelUtil.exportExcel(list, "证书管理", CanteenCertificatesVo.class, response);
    }

    /**
     * 获取证书管理详细信息
     *
     * @param certificatesId 主键
     */
    @SaCheckPermission("canteen:certificates:query")
    @GetMapping("/{certificatesId}")
    public R<CanteenCertificatesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long certificatesId) {
        return R.ok(canteenCertificatesService.queryById(certificatesId));
    }

    /**
     * 新增证书管理
     */
    @SaCheckPermission("canteen:certificates:add")
    @Log(title = "证书管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CanteenCertificatesBo bo) {
        return toAjax(canteenCertificatesService.insertByBo(bo));
    }

    /**
     * 修改证书管理
     */
    @SaCheckPermission("canteen:certificates:edit")
    @Log(title = "证书管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CanteenCertificatesBo bo) {
        return toAjax(canteenCertificatesService.updateByBo(bo));
    }

    /**
     * 删除证书管理
     *
     * @param certificatesIds 主键串
     */
    @SaCheckPermission("canteen:certificates:remove")
    @Log(title = "证书管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{certificatesIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] certificatesIds) {
        return toAjax(canteenCertificatesService.deleteWithValidByIds(List.of(certificatesIds), true));
    }
}
